﻿Imports System.Data.SQLite
Imports Sunts.DataLayer.SQLite

Public Class BackupLog
    Public Property BackupScheduleID As Int32
    Public Property BackupJobID As Int32
    Public Property JobName As String
    Public Property Progress As String
    Public Property ActualStart As DateTime?
    Public Property ActualEnd As DateTime?
    Public Property ScheduleDateTime As DateTime
    Public Property CompletionStatus As String

    Public Shared Function SelectActiveJobs(ConnectionString As String) As BackupLogCollection
        Dim BackupLogCollection As BackupLogCollection
        Dim BackupLog As BackupLog
        Dim Reader As SQLiteDataReader

        Try
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "SELECT * FROM vwActiveJobs"

                BackupLogCollection = New BackupLogCollection

                Reader = sdl.ExecuteDataReader

                While Reader.Read
                    BackupLog = New BackupLog
                    BackupLog.BackupScheduleID = CInt(Reader("BackupScheduleID"))
                    BackupLog.BackupJobID = CInt(Reader("BackupJobID"))
                    BackupLog.JobName = CStr(IIf(IsDBNull(Reader("JobName")), String.Empty, Reader("JobName")))
                    BackupLog.Progress = CStr(IIf(IsDBNull(Reader("Progress")), String.Empty, Reader("Progress")))
                    BackupLog.ActualStart = CDate(IIf(IsDBNull(Reader("ActualStart")), Nothing, Reader("ActualStart")))
                    BackupLog.ActualEnd = CDate(IIf(IsDBNull(Reader("ActualEnd")), Nothing, Reader("ActualEnd")))
                    BackupLog.ScheduleDateTime = CDate(Reader("ScheduleDateTime"))
                    BackupLogCollection.Add(BackupLog)
                End While
                Reader.Close()
            End Using

            SelectActiveJobs = BackupLogCollection

        Catch ex As Exception
            Throw New Exception("BackupLog: SelectActiveJobs()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            BackupLogCollection = Nothing
            BackupLog = Nothing
            Reader = Nothing
        End Try
    End Function

    Public Shared Function SelectCompletedJobs(ConnectionString As String) As BackupLogCollection
        Dim BackupLogCollection As BackupLogCollection
        Dim BackupLog As BackupLog
        Dim Reader As SQLiteDataReader

        Try
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "SELECT * FROM vwCompletedJobs LIMIT 100"

                BackupLogCollection = New BackupLogCollection

                Reader = sdl.ExecuteDataReader

                While Reader.Read
                    BackupLog = New BackupLog
                    BackupLog.BackupScheduleID = CInt(Reader("BackupScheduleID"))
                    BackupLog.JobName = CStr(IIf(IsDBNull(Reader("JobName")), String.Empty, Reader("JobName")))
                    BackupLog.Progress = CStr(IIf(IsDBNull(Reader("Progress")), String.Empty, Reader("Progress")))
                    BackupLog.ActualStart = CDate(IIf(IsDBNull(Reader("ActualStart")), Nothing, Reader("ActualStart")))
                    BackupLog.ActualEnd = CDate(IIf(IsDBNull(Reader("ActualEnd")), Nothing, Reader("ActualEnd")))
                    BackupLog.ScheduleDateTime = CDate(Reader("ScheduleDateTime"))
                    BackupLog.CompletionStatus = CStr(IIf(IsDBNull(Reader("CompletionStatus")), String.Empty, Reader("CompletionStatus")))

                    BackupLogCollection.Add(BackupLog)
                End While
                Reader.Close()
            End Using

            SelectCompletedJobs = BackupLogCollection

        Catch ex As Exception
            Throw New Exception("BackupLog: SelectCompletedJobs()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            BackupLogCollection = Nothing
            BackupLog = Nothing
            Reader = Nothing
        End Try
    End Function
End Class

Public Class BackupLogCollection
    Inherits System.Collections.ObjectModel.Collection(Of BackupLog)
End Class